//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartPolarView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartPolarView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartPolarView

IMPLEMENT_DYNCREATE(CChartPolarView, CBCGPChartExampleView)

CChartPolarView::CChartPolarView()
	: CBCGPChartExampleView(CChartPolarView::IDD)
{
	//{{AFX_DATA_INIT(CChartPolarView)
	m_nLineStyle = 0;
	m_nLineWidth = 0;
	m_bShowDataLabels = FALSE;
	m_nDataLabelAngle = 3;
	m_nCurveType = 2;
	//}}AFX_DATA_INIT

	m_nLineWidth = theApp.GetDefaultLineWidth() - 1;
}

CChartPolarView::~CChartPolarView()
{
}

void CChartPolarView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartPolarView)
	DDX_Control(pDX, IDC_DATA_LABEL_ANGLE, m_wndDataLabelAngle);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_CBIndex(pDX, IDC_LINE_STYLE, m_nLineStyle);
	DDX_CBIndex(pDX, IDC_LINE_WIDTH, m_nLineWidth);
	DDX_Check(pDX, IDC_DATA_LABELS, m_bShowDataLabels);
	DDX_CBIndex(pDX, IDC_DATA_LABEL_ANGLE, m_nDataLabelAngle);
	DDX_CBIndex(pDX, IDC_CURVE_TYPE, m_nCurveType);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartPolarView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartPolarView)
	ON_CBN_SELENDOK(IDC_CURVE_TYPE, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_LINE_WIDTH, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_LINE_STYLE, OnUpdateChart)
	ON_BN_CLICKED(IDC_DATA_LABELS, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_DATA_LABEL_ANGLE, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartPolarView diagnostics

#ifdef _DEBUG
void CChartPolarView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartPolarView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartPolarView message handlers

void CChartPolarView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->SetChartType(BCGPChartPolar);

	CBCGPChartSeries* pSeries1 = pChart->CreateSeries(_T("Series 1"));
	CBCGPChartSeries* pSeries2 = pChart->CreateSeries(_T("Series 2"));

	if (m_Feature == CDemoFeature::BCGP_Radar)
	{
		pSeries1->AddDataPoint(15.);
		pSeries1->AddDataPoint(10.);
		pSeries1->AddDataPoint(8.);
		pSeries1->AddDataPoint(22.);

		pSeries2->AddDataPoint(9.);
		pSeries2->AddDataPoint(15.);
		pSeries2->AddDataPoint(19.);
		pSeries2->AddDataPoint(21.);
		pSeries2->AddDataPoint(23.);
	}
	else
	{
		pSeries1->AddDataPoint(120., 0.);
		pSeries1->AddDataPoint(110., 30.);
		pSeries1->AddDataPoint(180., 60.);
		pSeries1->AddDataPoint(145, 90.);
		pSeries1->AddDataPoint(145, 120.);
		pSeries1->AddDataPoint(100, 180.);
		pSeries1->AddDataPoint(190, 225.);
		pSeries1->AddDataPoint(120, 270.);
		pSeries1->AddDataPoint(130, 300.);

		pSeries2->AddDataPoint(220., 0.);
		pSeries2->AddDataPoint(80., 30.);
		pSeries2->AddDataPoint(100., 60.);
		pSeries2->AddDataPoint(70, 90.);
		pSeries2->AddDataPoint(20, 120.);
		pSeries2->AddDataPoint(200, 150.);
		pSeries2->AddDataPoint(210, 180.);
		pSeries2->AddDataPoint(110, 210.);
		pSeries2->AddDataPoint(150, 270.);
		pSeries2->AddDataPoint(170, 300.);
		pSeries2->AddDataPoint(120, 330.);
	}

	pChart->SetThemeOpacity(30);

	CBCGPChartAxisPolarX* pXAxis = DYNAMIC_DOWNCAST(CBCGPChartAxisPolarX, pChart->GetChartAxis(BCGP_CHART_X_POLAR_AXIS));
	ASSERT_VALID(pXAxis);

	pXAxis->m_majorTickMarkType = CBCGPChartAxis::TMT_CROSS;

	CBCGPChartAxisPolarY* pYAxis = DYNAMIC_DOWNCAST(CBCGPChartAxisPolarY, pChart->GetChartAxis(BCGP_CHART_Y_POLAR_AXIS));
	ASSERT_VALID(pYAxis);

	pYAxis->m_majorTickMarkType = CBCGPChartAxis::TMT_CROSS;

	if (m_Feature == CDemoFeature::BCGP_Radar)
	{
		pYAxis->m_bRadialGridLines = FALSE;
	}

	OnUpdateChart();
}

void CChartPolarView::OnUpdateChart() 
{
	UpdateData();

	m_wndDataLabelAngle.EnableWindow(m_bShowDataLabels);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->ShowDataMarkers();

	double dblDataLabelAngle = (m_nDataLabelAngle - 2) * 45;

	pChart->ShowDataLabels(m_bShowDataLabels, TRUE, TRUE, dblDataLabelAngle);

	for (int i = 0; i < pChart->GetSeriesCount(); i++)
	{
		CBCGPChartPolarSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartPolarSeries, pChart->GetSeries(i));
		ASSERT_VALID(pSeries);

		BCGPChartFormatSeries style = pSeries->GetSeriesFormat();

		switch (m_nCurveType)
		{
		case 0:
			style.m_curveType = BCGPChartFormatSeries::CCT_NO_LINE;
			break;

		case 1:
			style.m_curveType = BCGPChartFormatSeries::CCT_LINE;
			break;

		case 2:
			style.m_curveType = BCGPChartFormatSeries::CCT_SPLINE;
			break;

		case 3:
			style.m_curveType = BCGPChartFormatSeries::CCT_SPLINE_HERMITE;
			break;
		}

		style.SetSeriesOutlineDashStyle((CBCGPStrokeStyle::BCGP_DASH_STYLE)m_nLineStyle);
		style.SetSeriesLineWidth(m_nLineWidth + 1);

		// Don't close and don't fill second series:
		pSeries->CloseShape(i == 0, i == 0);

		pSeries->SetSeriesFormat(style);
	}

	OnChartAnimation();
}
